package ua.com.minus1.lookbattle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import ua.com.minus1.lookbattle.menu.ClicksActionView;
import ua.com.minus1.lookbattle.utils.AppPreferences;
import ua.com.minus1.lookbattle.utils.BundleValues;

/**
 * Created with IntelliJ IDEA.
 * User: Denis
 * Date: 17.10.12
 * Time: 21:47
 * To change this template use File | Settings | File Templates.
 */
public class BattleActivity extends SherlockFragmentActivity implements BattleFragment.Callbacks {
    private int mCatId;
    private ViewPager mViewPager;
    private BattlePagerAdapter mAdapter;
    private AppPreferences mPreferences;
    private ClicksActionView mClicksActionView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battle);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mCatId = extras.getInt(BundleValues.CAT_ID);
        }

        mPreferences = new AppPreferences(this);

        setupViews();
        mViewPager.setCurrentItem(CatItemFactory.getCatPositionId(mCatId), false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mClicksActionView != null)
            mClicksActionView.setClicks(mPreferences.getClicks());
    }

    private void setupViews() {
        mViewPager = (ViewPager) findViewById(R.id.battle_pager);
        mAdapter = new BattlePagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.battle, menu);
        mClicksActionView = (ClicksActionView) menu.findItem(R.id.menu_battle_clicks).getActionView();
        if(mPreferences != null)
            mClicksActionView.setClicks(mPreferences.getClicks());
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_my_looks:
                openMyLooks();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void openMyLooks() {
        Bundle extras = new Bundle();
        extras.putInt(BundleValues.CAT_ID, CatItemFactory.getCatIdOnPosition(mViewPager.getCurrentItem()));
        Intent intent = new Intent(this, mPreferences.getRegistered()? LooksActivity.class: LoginActivity.class);
        intent.putExtras(extras);
        startActivity(intent);
    }

    public void onClicksUpdate(int clicks) {
        mPreferences.setClicks(clicks);
        if(mClicksActionView != null)
            mClicksActionView.setClicks(clicks);
    }

    private class BattlePagerAdapter extends FragmentStatePagerAdapter{
        private CatItemFactory.CatItem[] mCats;

        private BattlePagerAdapter(FragmentManager fm) {
            super(fm);
            mCats = CatItemFactory.createItems(BattleActivity.this);
        }

        public CatItemFactory.CatItem getItemData(int i) {
            return mCats[i];
        }

        @Override
        public Fragment getItem(int i) {
            BattleFragment fragment = BattleFragment.newInstance(getItemData(i).catId);
            return fragment;
        }

        @Override
        public int getCount() {
            return CatItemFactory.getCatsCount();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mCats[position].label.toUpperCase();
        }
    }

}
